package zq.cn.ioapp;

import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences sp;
    private SQLiteDatabase sqd;
    private int i=0;
    //设计SQliteOpenHelper来自动处理第一次表的创建
    private MyOpenHelper moh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = PreferenceManager.getDefaultSharedPreferences(this);
//通过SQLiteDatabase来操作数据库SQlite
        //需要配置<uses-permission>
        File file = Environment.getExternalStorageDirectory();
        Log.i("zq", file.getAbsolutePath() + ",,,,,,,");
        moh = new MyOpenHelper(this,file.getAbsolutePath()+"/zq.db",null,1);
        sqd = moh.getWritableDatabase();
        //在实体机上读取，虚拟机只在c盘下创建
//        sqd = SQLiteDatabase.openOrCreateDatabase(file.getAbsolutePath()+"/zq.db",null);

    }
//通过File的IO操作读写数据
    public void IOStory(View view) throws Exception{
        OutputStream ous = openFileOutput("a.txt", MODE_PRIVATE | MODE_APPEND);
        ous.write("this is a good JOB 哈哈哈哈".getBytes("UTF-8"));
        ous.close();
    }
    public void getIOStory(View view) throws Exception{
        InputStream ins = openFileInput("a.txt");
        byte[] bytes = new byte[ins.available()];
        ins.read(bytes);
        Log.i("zq", new String(bytes, "UTF-8"));
    }
    //利用SharedPreference读写数据
    public void StoryPref(View view) {
        Set<String> set = new HashSet<String>();
        set.add("set1");
        set.add("set2");
        set.add("set3");
//执行完成后会生成：包全名_preferences.xml.以Map形式存储。
        SharedPreferences.Editor editor = sp.edit();

        editor.putString("zq0","yyb");
        editor.putInt("zq1", 200);
        editor.putStringSet("zq2", set);

        editor.commit();
    }

    public void getStoryPref(View view) {
        String zq0 = sp.getString("zq0", null);
        int zq1 = sp.getInt("zq1",-1);
        Set<String> zq2 = sp.getStringSet("zq2",null);

        Log.i("zq", zq0 + ".........." + zq1);
        for(String se:zq2) {
            Log.i("zq",se+"..............");
        }

    }
    //使用SQLiteOpenHelper时，表的创建操作放在SQLiteOpenHelper子类中进行
//    public void StoryCreateSQlite(View view) {
//        String ddl = "create table users(_id integer primary key autoincrement,uname text,age integer);";
//        sqd.execSQL(ddl);
//        sqd.close();
//    }

    public void StorySQlite(View view) {
        String sql = "insert into users(uname,age) values(?,?);";
        try {
            sqd.beginTransaction();

            sqd.execSQL(sql,new Object[]{"sql"+(i++),10+i});
            sqd.setTransactionSuccessful();
        }catch(Exception e) {

        }finally {
            sqd.endTransaction();
            sqd.close();
        }
    }

    public void getSQlite(View view) {
        String sql = "select _id,uname,age from users where _id=?;";
        Cursor cursor = sqd.rawQuery(sql, new String[]{"1"});
        while(cursor.moveToNext()) {
            String str = cursor.getString(1);//栏位由0开始
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            Log.i("zq",str+".........."+id);
        }
        sqd.close();
    }

    public void query(View view) {
        Uri uri = Uri.parse("content://zq.cn");
        ContentResolver ctr = getContentResolver();
        Cursor cursor = ctr.query(uri,null,null,null,null);
    }
}
